<template>
  <div class="components-panel">
    <div class="panel-header">
      <span class="panel-title active">新增模块</span>
      <span class="panel-title">我的模块</span>
    </div>
    
    <!-- 常用组件 -->
    <div class="component-category">
      <div class="category-name">
        常用组件
        <i class="el-icon-arrow-down"></i>
      </div>
      <div class="components-grid">
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'text')">
          <div class="component-icon">
            <i class="el-icon-document"></i>
          </div>
          <div class="component-label">文本</div>
        </div>
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'image')">
          <div class="component-icon">
            <i class="el-icon-picture"></i>
          </div>
          <div class="component-label">图片</div>
        </div>
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'button')">
          <div class="component-icon">
            <i class="el-icon-caret-right"></i>
          </div>
          <div class="component-label">按钮</div>
        </div>
      </div>
    </div>
    
    <!-- 基础组件 -->
    <div class="component-category">
      <div class="category-name">
        基础组件
        <i class="el-icon-arrow-down"></i>
      </div>
      <div class="components-grid">
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'grid')">
          <div class="component-icon">
            <i class="el-icon-more"></i>
          </div>
          <div class="component-label">魔方导航</div>
        </div>
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, '图文展示')">
          <div class="component-icon">
            <i class="el-icon-s-marketing"></i>
          </div>
          <div class="component-label">图文展示</div>
        </div>
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'list')">
          <div class="component-icon">
            <i class="el-icon-menu"></i>
          </div>
          <div class="component-label">列表多图</div>
        </div>
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'carousel')">
          <div class="component-icon">
            <i class="el-icon-refresh"></i>
          </div>
          <div class="component-label">轮播多图</div>
        </div>
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'article')">
          <div class="component-icon">
            <i class="el-icon-notebook-1"></i>
          </div>
          <div class="component-label">文章列表</div>
        </div>
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'video')">
          <div class="component-icon">
            <i class="el-icon-video-camera"></i>
          </div>
          <div class="component-label">视频</div>
        </div>
      </div>
    </div>
    
    <!-- 排版组件 -->
    <div class="component-category">
      <div class="category-name">
        排版组件
        <i class="el-icon-arrow-down"></i>
      </div>
      <div class="components-grid">
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'tags')">
          <div class="component-icon">
            <i class="el-icon-collection-tag"></i>
          </div>
          <div class="component-label">横向标签</div>
        </div>
      </div>
    </div>
    
    <!-- 互动组件 -->
    <div class="component-category">
      <div class="category-name">
        互动
        <i class="el-icon-arrow-down"></i>
      </div>
      <div class="components-grid">
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'search')">
          <div class="component-icon">
            <i class="el-icon-search"></i>
          </div>
          <div class="component-label">搜索</div>
        </div>
        <div class="component-item" draggable="true" @dragstart="handleDragStart($event, 'notice')">
          <div class="component-icon">
            <i class="el-icon-bell"></i>
          </div>
          <div class="component-label">公告</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ComponentsPanel',
  methods: {
    handleDragStart(event, componentType) {
      this.$emit('drag-start', event, componentType);
      
      // 添加拖拽时的视觉效果
      const element = event.target;
      element.classList.add('dragging');
      
      // 监听拖拽结束事件移除样式
      const handleDragEnd = () => {
        element.classList.remove('dragging');
        event.target.removeEventListener('dragend', handleDragEnd);
      };
      
      event.target.addEventListener('dragend', handleDragEnd);
    }
  }
};
</script>

<style scoped>
.components-panel {
  width: 250px;
  background-color: #fff;
  border-right: 1px solid #e8e8e8;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.panel-header {
  display: flex;
  padding: 20px;
  border-bottom: 1px solid #e8e8e8;
}

.panel-title {
  margin-right: 20px;
  cursor: pointer;
  color: #666;
  font-size: 14px;
  padding-bottom: 5px;
}

.panel-title.active {
  color: #1890ff;
  font-weight: 500;
  border-bottom: 2px solid #1890ff;
}

.component-category {
  padding: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.category-name {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  font-size: 14px;
  color: #333;
  font-weight: 500;
  cursor: pointer;
}

.category-name i {
  margin-left: 5px;
  font-size: 12px;
  transition: transform 0.3s;
}

.category-name.collapsed i {
  transform: rotate(-90deg);
}

.components-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.component-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 5px;
  cursor: grab;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  transition: all 0.3s;
  background-color: #fff;
}

.component-item:hover {
  border-color: #1890ff;
  box-shadow: 0 0 5px rgba(24, 144, 255, 0.2);
  transform: translateY(-2px);
}

.component-item.dragging {
  opacity: 0.5;
  transform: scale(0.95);
}

.component-item:active {
  cursor: grabbing;
}

.component-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 5px;
  background-color: #f0f7ff;
  border-radius: 4px;
  font-size: 18px;
  color: #1890ff;
}

.component-label {
  font-size: 12px;
  color: #666;
  text-align: center;
  word-break: break-all;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .components-panel {
    width: 200px;
  }
  
  .components-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .components-panel {
    width: 100%;
    height: auto;
    max-height: 300px;
  }
}
</style>